package com.zr.d_c.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zr.d_c.model.Dish;
import com.zr.d_c.service.DishService;
import com.zr.d_c.service.OrderService;
import com.zr.d_c.serviceimpl.DishServiceImpl;
import com.zr.d_c.serviceimpl.OrderServiceImpl;
/**
 * 訂單撤銷
 * @author Administrator
 *
 */
public class RevokeCurrentOrderController extends HttpServlet {

	OrderService orderservice = new OrderServiceImpl();
	DishService dishservice = new DishServiceImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter pw = resp.getWriter();
		String oidstr = req.getParameter("oid");
		String odishiesstr = req.getParameter("odishies");
		String sidstr = req.getParameter("sid");
		
		int sid = Integer.parseInt(sidstr);
		int oid = Integer.parseInt(oidstr);
		String[] oidshiesst = odishiesstr.split(",");
		if(orderservice.modifyOtemp1ByOid(oid)>0){
			for (int i = 0; i < oidshiesst.length; i++) {
				String[] oidshiess = oidshiesst[i].split(":");
				for (int j = 0; j < oidshiess.length-1; j+=2) {
					String dname = oidshiess[j];
					int bnum = Integer.parseInt(oidshiess[j+1].substring(0, 1));
					Dish d = dishservice.getDishBySidAndDname(sid, dname);
					dishservice.modifyDnum(d.getDid(), d.getDnum()+bnum);
				}
			}
			pw.write("1");
		}else{
			pw.write("0");
		}
		pw.close();
		

	}
}
